/* 
 * File:   index.cxx
 * Author: sharea
 *
 * Created on 14. Juni 2011, 19:01
 */

//System Includes
#include <memory>
#include <iostream>
#include <string>
#include <stdlib.h>

//ODB Lib
#include <odb/database.hxx>
#include <odb/mysql/database.hxx>
#include <odb/transaction.hxx>

#include "database.hxx"

//Template Lib
#include <ctemplate/template.h>
#include <ctemplate/template_dictionary.h>

//Persistenz Entities
#include "artikel.hxx"
#include "artikel-odb.hxx"

using namespace std;
using namespace odb::core;

/*
 * 
 */
int main(int argc, char* argv[]) {

    //HTTP Header
    cout << "Content-type: text/html" << endl << endl << endl;
    
    ctemplate::TemplateDictionary dict("example");
    dict.SetValue("NAME", "Testuser");
    
    std::string output;
    ctemplate::ExpandTemplate("index.tpl", ctemplate::DO_NOT_STRIP, &dict, &output);
    
    cout << output << endl;
    
    try {
        auto_ptr<odb::database> db (
          new odb::mysql::database (
            "opencpp",     // database login name
            "start123", // database password
            "opencpp", // database name
			"sharea.de"
            ));

        typedef odb::query<artikel> query;
        typedef odb::result<artikel> result;

        {
            transaction t(db->begin());

            result r(db->query<artikel > (query::id < 3));

            for (result::iterator i(r.begin()); i != r.end(); ++i) {
                cout << "<p>Titel: " << i->title() << " Beschreibung: " << i->desc() << "</p>" << endl;
            }

            t.commit();
        }

    }    catch (const odb::exception& e) {
        cerr << e.what() << endl;
        return 1;
    }

    //Hier kommt dann das Templating rein


    return 0;
}

